{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2024-04-07T08:49:53.918812Z",
     "start_time": "2024-04-07T08:49:52.407302Z"
    }
   },
   "source": [
    "import torch\n",
    "\n",
    "# 定义一个简单的函数\n",
    "def simple_function(x, y):\n",
    "    return 3*x**2 + 2*y\n",
    "\n",
    "# 创建输入张量\n",
    "x = torch.tensor(2.0, requires_grad=True)\n",
    "y = torch.tensor(3.0, requires_grad=True)\n",
    "\n",
    "# 计算函数的输出\n",
    "output = simple_function(x, y)\n",
    "\n",
    "# 计算输出关于输入的梯度\n",
    "gradients = torch.autograd.grad(output, (x, y))\n",
    "\n",
    "# 打印梯度\n",
    "print(\"Gradient with respect to x:\", gradients[0])\n",
    "print(\"Gradient with respect to y:\", gradients[1])\n"
   ],
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Gradient with respect to x: tensor(12.)\n",
      "Gradient with respect to y: tensor(2.)\n"
     ]
    }
   ],
   "execution_count": 1
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-04-07T09:28:25.028800Z",
     "start_time": "2024-04-07T09:28:25.021356Z"
    }
   },
   "cell_type": "code",
   "source": "gradients",
   "id": "a1969af4f207dd79",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(tensor(12.), tensor(2.))"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 3
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-04-07T09:32:38.133606Z",
     "start_time": "2024-04-07T09:32:38.126621Z"
    }
   },
   "cell_type": "code",
   "source": [
    "yy = torch.tensor([[1,2,3,4,5], [6,7,8,9,10]])\n",
    "xx = torch.tensor([[121,2,3,4,5], [6,7,8,93,10]])\n",
    "torch.cat((xx, yy), dim=1)"
   ],
   "id": "64d0fe8ff1e8fd63",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[121,   2,   3,   4,   5,   1,   2,   3,   4,   5],\n",
       "        [  6,   7,   8,  93,  10,   6,   7,   8,   9,  10]])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 10
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
